﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web.Configuration;
using System.Web;
using System.Data.SqlClient;

namespace TestJQGrid
{   
    public class DataLayer
    {
        #region Global Variables
        #endregion

        private string GetConnectionString()
        {
            string _connectionStringsName = "AgentBillingConnectionString";
            System.Configuration.ConnectionStringSettingsCollection config = System.Configuration.ConfigurationManager.ConnectionStrings;
            for (int i = 0; i < config.Count; i++)
            {
                if (config[i].Name.Equals(_connectionStringsName, StringComparison.OrdinalIgnoreCase))
                    return config[i].ToString();
            }
            return String.Empty;
        }

        public DataTable getDataSource(string query)
        {
            DataTable dt=new DataTable();
            SqlConnection sqlC = new SqlConnection(GetConnectionString());

            try
            {
                sqlC.Open();
                SqlCommand cmd = new SqlCommand(query, sqlC);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
            }
            catch (SqlException ex)
            {
            }
            finally
            {
                sqlC.Close();
            }

            return dt;
        }

        public object insertScaler(string query)
        {
            object result = null;
            SqlConnection sqlc = new SqlConnection(GetConnectionString());

            try
            {
                sqlc.Open();
                SqlCommand cmd = new SqlCommand(query, sqlc);
                result = (object)cmd.ExecuteScalar();
            }
            catch (Exception )
            {

            }
            finally
            {
                sqlc.Close();
            }

            return result;
        }

        public int insertQuery(string query)
        {
            int result = 0;
            SqlConnection sqlc = new SqlConnection(GetConnectionString());

            try
            {
                sqlc.Open();
                SqlCommand cmd = new SqlCommand(query, sqlc);
                result = cmd.ExecuteNonQuery();
            }
            catch (Exception )
            {

            }
            finally
            {
                sqlc.Close();
            }

            return result;
        }
    }
}